#include <bits/stdc++.h>
using namespace std;

int n;
string s;
int v = 0;
long long x = 0;
int flag = 0;
int last_v = 0;

int main()
{
    scanf("%d", &n);
    cin >> s;

    for (int i = 0; i < n; i++)
    {
        if (s[i] == '0')
        {
            if (flag == 0)
            {
                v += 10;
            }
            x += v;
        }
        else if (s[i] == '1')
        {
            if (flag == 0)
            {
                if (v >= 5)
                {
                    v -= 5;
                }
                else
                {
                    v = 0;
                }
            }
            x += v;
        }
        else if (s[i] == '2')
        {
            if (flag == 0)
            {
                flag = 1;
                last_v = v;
                v -= 10;
                if (v < 0)
                    v = 0;
            }
            x += v;
        }

        if (flag == 1)
        {
            flag = 0;
            v = last_v;
        }
    }

    printf("%lld\n", x);
    return 0;
}